package com.geek.factory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geek.factory.dto.ProductInStockDTO;
import com.geek.factory.entity.Product;
import com.geek.factory.entity.ProductStock;
import com.geek.factory.mapper.ProductMapper;
import com.geek.factory.mapper.ProductStockMapper;
import com.geek.factory.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    private static final String CACHE_NAME = "product";

//    @Override
//    @Cacheable(value = CACHE_NAME, key = "#id", unless = "#result == null")
//    public Product getById(Serializable id) {
//        log.info("从数据库查询产品信息, id: {}", id);
//        return super.getById(id);
//    }
@Override
@Cacheable(value = CACHE_NAME, key = "#id", unless = "#result == null")
public Product getById(Serializable id) {
    log.info("从数据库查询产品信息, id: {}", id);
    if (id instanceof Integer) {
        return super.getById(((Integer) id).longValue());  // Integer -> Long
    }
    return super.getById(id);
}

    @Override
    @CachePut(value = CACHE_NAME, key = "#product.id")
    public boolean save(Product product) {
        boolean result = super.save(product);
        log.info("新增产品信息并写入缓存, id: {}, result: {}", product.getId(), result);
        return result;
    }

    @Override
    @CachePut(value = CACHE_NAME, key = "#product.id")
    public boolean updateById(Product product) {
        boolean result = super.updateById(product);
        log.info("更新产品信息并更新缓存, id: {}, result: {}", product.getId(), result);
        return result;
    }

    @Override
    @CacheEvict(value = CACHE_NAME, key = "#id")
    public boolean removeById(Serializable id) {
        boolean result = super.removeById(id);
        log.info("删除产品信息并删除缓存, id: {}, result: {}", id, result);
        return result;
    }

    //
    @Autowired
    private ProductStockMapper productStockMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public void inStock(ProductInStockDTO dto) {
        Product product = this.getById(dto.getProductId());
        // ❗ status 必须是 "1" 表示已完成
        if (product == null || !"1".equals(product.getStatus())) {
            throw new RuntimeException("产品不存在或未完成，无法入库！");
        }
        ProductStock stock = productStockMapper.selectByProductId(dto.getProductId());
        if (stock == null) {
            stock = new ProductStock();
            stock.setProductId(dto.getProductId());
            stock.setQuantity(dto.getQuantity());
            productStockMapper.insert(stock);
        } else {
            stock.setQuantity(stock.getQuantity() + dto.getQuantity());
            productStockMapper.updateById(stock);
        }
    }

    @Override
    public List<Product> getCompletedProducts() {
        return productMapper.selectList(
                new QueryWrapper<Product>()
                        .isNotNull("complete_time")
                        .eq("status", "1")  // ✅ 改为 "1"，表示已完成
        );

    }

    @Override
    public Product getByCode(String code) {
        return baseMapper.selectOne(new QueryWrapper<Product>().eq("code", code));
    }

}
